
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>027XML解析 · 书名</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="作者">
        <meta name="identifier" content="图书编号" scheme="ISBN">
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-intopic-toc/style.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-flexible-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="028 注解.html" />
    
    
    <link rel="prev" href="026网络编程Socket.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    书名
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Books</li>
        
        
    
        <li class="chapter " data-level="2.1" >
            
                <span>
            
                    
                    Java
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1" data-path="001环境变量.html">
            
                <a href="001环境变量.html">
            
                    
                    001环境变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2" data-path="002Java初体验.html">
            
                <a href="002Java初体验.html">
            
                    
                    002Java初体验
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3" data-path="003变量命名及数据类型.html">
            
                <a href="003变量命名及数据类型.html">
            
                    
                    003变量命名及数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.4" data-path="004运算符.html">
            
                <a href="004运算符.html">
            
                    
                    004运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.5" data-path="005选择结构.html">
            
                <a href="005选择结构.html">
            
                    
                    005选择结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.6" data-path="006循环结构.html">
            
                <a href="006循环结构.html">
            
                    
                    006循环结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.7" data-path="007数组.html">
            
                <a href="007数组.html">
            
                    
                    007数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.8" data-path="008类和对象方法.html">
            
                <a href="008类和对象方法.html">
            
                    
                    008类和对象方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.9" data-path="009构造方法.html">
            
                <a href="009构造方法.html">
            
                    
                    009构造方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.10" data-path="010封装与关键字static.html">
            
                <a href="010封装与关键字static.html">
            
                    
                    010封装与关键字Static
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.11" data-path="011 继承.html">
            
                <a href="011 继承.html">
            
                    
                    011 继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.12" data-path="012方法重写.html">
            
                <a href="012方法重写.html">
            
                    
                    012方法重写
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.13" data-path="013多态.html">
            
                <a href="013多态.html">
            
                    
                    013多态
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.14" data-path="014抽象类抽象方法.html">
            
                <a href="014抽象类抽象方法.html">
            
                    
                    014抽象类抽象方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.15" data-path="015接口.html">
            
                <a href="015接口.html">
            
                    
                    015接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.16" data-path="016异常.html">
            
                <a href="016异常.html">
            
                    
                    016异常
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.17" data-path="017集合.html">
            
                <a href="017集合.html">
            
                    
                    017集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.18" data-path="017集合2.html">
            
                <a href="017集合2.html">
            
                    
                    017集合2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.19" data-path="018泛型.html">
            
                <a href="018泛型.html">
            
                    
                    018泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.20" data-path="019Collections算法类.html">
            
                <a href="019Collections算法类.html">
            
                    
                    019Collections算法类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.21" data-path="020枚举.html">
            
                <a href="020枚举.html">
            
                    
                    020枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.22" data-path="021包装类.html">
            
                <a href="021包装类.html">
            
                    
                    021包装类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.23" data-path="022常用类.html">
            
                <a href="022常用类.html">
            
                    
                    022常用类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.24" data-path="023File类.html">
            
                <a href="023File类.html">
            
                    
                    023File类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.25" data-path="024IO流.html">
            
                <a href="024IO流.html">
            
                    
                    024IO流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.26" data-path="025多线程.html">
            
                <a href="025多线程.html">
            
                    
                    025多线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.27" data-path="025多线程线程池.html">
            
                <a href="025多线程线程池.html">
            
                    
                    025多线程线程池
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.28" data-path="026网络编程Socket.html">
            
                <a href="026网络编程Socket.html">
            
                    
                    026网络编程Socket
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="2.1.29" data-path="027XML解析.html">
            
                <a href="027XML解析.html">
            
                    
                    027XML解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.30" data-path="028 注解.html">
            
                <a href="028 注解.html">
            
                    
                    028 注解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.31" data-path="029 Lambda表达式.html">
            
                <a href="029 Lambda表达式.html">
            
                    
                    029 Lambda表达式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.32" data-path="029 反射.html">
            
                <a href="029 反射.html">
            
                    
                    029 反射
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.2" >
            
                <span>
            
                    
                    Mybatis
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.2.1" data-path="../mybatis/MyBatis.html">
            
                <a href="../mybatis/MyBatis.html">
            
                    
                    MyBatis
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.2.1.1" >
            
                <span>
            
                    
                    MyBatis
            
                </span>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.3" >
            
                <span>
            
                    
                    Mysql
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.3.1" data-path="../mysql/01 SQL通用语法.html">
            
                <a href="../mysql/01 SQL通用语法.html">
            
                    
                    01 SQL通用语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.2" data-path="../mysql/02 DDL.html">
            
                <a href="../mysql/02 DDL.html">
            
                    
                    02 DDL
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.3" data-path="../mysql/03 DML.html">
            
                <a href="../mysql/03 DML.html">
            
                    
                    03 DML
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.4" data-path="../mysql/04 DQL.html">
            
                <a href="../mysql/04 DQL.html">
            
                    
                    04 DQL
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.5" data-path="../mysql/05 DCL.html">
            
                <a href="../mysql/05 DCL.html">
            
                    
                    05 DCL
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.6" data-path="../mysql/06 函数.html">
            
                <a href="../mysql/06 函数.html">
            
                    
                    06 函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.7" data-path="../mysql/07 约束.html">
            
                <a href="../mysql/07 约束.html">
            
                    
                    07 约束
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.8" data-path="../mysql/08 多表查询.html">
            
                <a href="../mysql/08 多表查询.html">
            
                    
                    08 多表查询
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.9" data-path="../mysql/09 事务.html">
            
                <a href="../mysql/09 事务.html">
            
                    
                    09 事务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.10" data-path="../mysql/999zzz.html">
            
                <a href="../mysql/999zzz.html">
            
                    
                    999zzz
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.11" data-path="../mysql/MySql.html">
            
                <a href="../mysql/MySql.html">
            
                    
                    MySql
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.12" data-path="../mysql/sql.html">
            
                <a href="../mysql/sql.html">
            
                    
                    Sql
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.4" >
            
                <span>
            
                    
                    Spring
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.4.1" data-path="../spring/spring.html">
            
                <a href="../spring/spring.html">
            
                    
                    Spring
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.5" >
            
                <span>
            
                    
                    SpringMVC
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.5.1" data-path="../springMVC/springMVC.html">
            
                <a href="../springMVC/springMVC.html">
            
                    
                    SpringMVC
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.6" >
            
                <span>
            
                    
                    整合
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.6.1" data-path="../整合/Mybatis框架.html">
            
                <a href="../整合/Mybatis框架.html">
            
                    
                    Mybatis框架
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6.2" data-path="../整合/SpringBoot.html">
            
                <a href="../整合/SpringBoot.html">
            
                    
                    SpringBoot
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6.3" data-path="../整合/springmvc框架.html">
            
                <a href="../整合/springmvc框架.html">
            
                    
                    Springmvc框架
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6.4" data-path="../整合/spring框架.html">
            
                <a href="../整合/spring框架.html">
            
                    
                    Spring框架
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >027XML解析</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x89E3;&#x6790;-xml">&#x89E3;&#x6790; XML</h1>
<p>XML&#x89E3;&#x6790;&#x65B9;&#x5F0F;:</p>
<ol>
<li><strong>DOM</strong>: &#x8981;&#x6C42;&#x89E3;&#x6790;&#x5668;&#x5C06;&#x6574;&#x4E2A; XML &#x6587;&#x4EF6;&#x5168;&#x90E8;&#x52A0;&#x8F7D;&#x5230;&#x5185;&#x5B58;&#x4E2D;, &#x751F;&#x6210;&#x4E00;&#x4E2A; Document &#x5BF9;&#x8C61;<ol>
<li>&#x4F18;&#x70B9;: &#x5143;&#x7D20;&#x548C;&#x5143;&#x7D20;&#x4E4B;&#x95F4;&#x4FDD;&#x7559;&#x7ED3;&#x6784;, &#x5173;&#x7CFB;, &#x53EF;&#x4EE5;&#x9488;&#x5BF9;&#x5143;&#x7D20;&#x8FDB;&#x884C;&#x589E;&#x5220;&#x6539;&#x64CD;&#x4F5C;</li>
<li>&#x7F3A;&#x70B9;: &#x5982;&#x679C; XML &#x6587;&#x4EF6;&#x8FC7;&#x5927;,&#x53EF;&#x80FD;&#x9020;&#x6210;&#x5185;&#x5B58;&#x6EA2;&#x51FA;</li>
</ol>
</li>
<li><strong>SAX</strong>: &#x662F;&#x4E00;&#x79CD;&#x901F;&#x5EA6;&#x66F4;&#x5FEB;,&#x66F4;&#x52A0;&#x9AD8;&#x6548;&#x7684;&#x89E3;&#x6790;&#x65B9;&#x5F0F;, &#x5B83;&#x662F;&#x9010;&#x884C;&#x626B;&#x63CF;, &#x8FB9;&#x626B;&#x63CF;, &#x4FBF;&#x89E3;&#x6790;, &#x5E76;&#x4E14;&#x4EE5;&#x4E8B;&#x4EF6;&#x9A71;&#x52A8;&#x7684;&#x65B9;&#x5F0F;&#x8FDB;&#x884C;&#x5267;&#x5410;&#x7684;&#x89E3;&#x6790;, &#x6CA1;&#x89E3;&#x6790;&#x4E00;&#x884C;&#x90FD;&#x4F1A;&#x89E6;&#x53D1;&#x4E00;&#x4E2A;&#x4E8B;&#x4EF6;<ol>
<li>&#x4F18;&#x70B9;: &#x4E0D;&#x4F1A;&#x51FA;&#x73B0;&#x5185;&#x5B58;&#x6EA2;&#x51FA;&#x7684;&#x95EE;&#x9898;, &#x53EF;&#x4EE5;&#x5904;&#x7406;&#x5927;&#x6587;&#x4EF6;</li>
<li>&#x7F3A;&#x70B9;: &#x53EA;&#x80FD;&#x8BFB;, &#x4E0D;&#x80FD;&#x5199;</li>
</ol>
</li>
</ol>
<p>&#x89E3;&#x6790;&#x5668;&#x5C31;&#x662F;&#x66F4;&#x5177;&#x4E0D;&#x540C;&#x7684;&#x89E3;&#x6790;&#x65B9;&#x5F0F;&#x63D0;&#x4F9B;&#x5177;&#x4F53;&#x7684;&#x5B9E;&#x73B0;, &#x4E3A;&#x4E86;&#x65B9;&#x4FBF;&#x5F00;&#x53D1;&#x4EBA;&#x5458;&#x89E3;&#x6790;XML, &#x6709;&#x4E00;&#x4E9B;&#x65B9;&#x4FBF;&#x64CD;&#x4F5C;&#x7684;&#x7C7B;&#x5E93;</p>
<p><strong>dom4j</strong>:&#x6BD4;&#x8F83;&#x7B80;&#x5355;&#x7684;XML&#x89E3;&#x6790;&#x7C7B;&#x5E93;</p>
<p><strong>Jsoup</strong>:&#x529F;&#x80FD;&#x5F3A;&#x5927;&#x7684;DOM&#x65B9;&#x5F0F;&#x89E3;&#x6790;&#x7684;&#x7C7B;&#x5E93;, &#x5C24;&#x5176;&#x5BF9;HTML&#x7684;&#x89E3;&#x6790;&#x66F4;&#x52A0;&#x65B9;&#x4FBF;</p>
<h2 id="dom-&#x65B9;&#x5F0F;">dom &#x65B9;&#x5F0F;</h2>
<p>dom&#x89E3;&#x6790;XML&#x6B65;&#x9AA4;</p>
<ol>
<li>&#x8BFB;&#x53D6;XML&#x6587;&#x4EF6;</li>
<li>&#x4FEE;&#x6539;XML&#x6587;&#x4EF6;</li>
<li>&#x4FDD;&#x5B58;XMl&#x6587;&#x4EF6;</li>
</ol>
<p><strong>&#x8BFB;&#x53D6;XML</strong></p>
<p>&#x5BF9;XML&#x6587;&#x4EF6;&#x4E2D;&#x8BFB;&#x53D6;&#x7684;&#x5185;&#x5BB9;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5C06;&#x5B83;&#x8BFB;&#x53D6;&#x4E3A; Document &#x5BF9;&#x8C61;</p>
<p> Document &#x53EF;&#x4EE5;&#x5BF9;&#x8BFB;&#x53D6;&#x5230;&#x7684;&#x5185;&#x5BB9;&#x8FDB;&#x884C;&#x589E;&#x5220;&#x6539;&#x5404;&#x79CD;&#x64CD;&#x4F5C;</p>
<pre><code class="lang-txt">&#x83B7;&#x53D6;document&#x5BF9;&#x8C61;&#x7684;&#x6D41;&#x7A0B;
Document &lt;--- DocumentBuilder &lt;--- DocumentBuilderFactory
</code></pre>
<ul>
<li>&#x521B;&#x5EFA;&#x4E00;&#x4E2A;DocumentBuilderFactory&#x7684;&#x5BF9;&#x8C61;</li>
<li>&#x521B;&#x5EFA;&#x4E00;&#x4E2A;DocumentBuilder&#x5BF9;&#x8C61;</li>
<li>&#x901A;&#x8FC7;DocunmentBuilder&#x7684;parse(&#x2026;)&#x65B9;&#x6CD5;&#x5F97;&#x5230;Document&#x5BF9;&#x8C61;</li>
</ul>
<pre><code class="lang-java">DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(url); <span class="hljs-comment">// &#x6587;&#x4EF6;&#x8DEF;&#x5F84;</span>
</code></pre>
<p><strong>&#x5BF9; XML &#x8FDB;&#x884C;&#x8FDB;&#x884C;&#x589E;&#x5220;&#x6539;&#x64CD;&#x4F5C;</strong></p>
<blockquote>
<p>&#x89E3;&#x6790;&#x65F6;&#xFF0C;&#x4E3B;&#x8981;&#x4F7F;&#x7528;Document&#x7C7B;&#xFF0C;Node&#x7C7B;&#xFF0C;NodeList&#x7C7B;</p>
</blockquote>
<p><strong>Document&#x7C7B;&#x4E2D;&#x7684;&#x65B9;&#x6CD5;&#x4ECB;&#x7ECD;&#xFF1A;</strong></p>
<ul>
<li><p>getElementsByTagName(String tagName)
&#x53C2;&#x6570;tagName&#x8868;&#x793A;&#x5143;&#x7D20;&#x540D;&#xFF0C;&#x5982;&#x4E0A;&#x8FF0;&#x4ECB;&#x7ECD;xml&#x6587;&#x4EF6;&#x7ED3;&#x6784;&#x65F6;&#x7684;book&#x3001;bookstore&#x7B49;&#xFF0C;&#x8BE5;&#x65B9;&#x6CD5;&#x5C06;&#x4F1A;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;NodeList&#x5BF9;&#x8C61;&#xFF0C;&#x5176;&#x4E2D;&#x5305;&#x542B;&#x4E86;&#x6587;&#x6863;&#x4E2D;&#x6240;&#x6709;&#x5143;&#x7D20;&#x540D;&#x4E3A;tagName&#x7684;&#x8282;&#x70B9;&#x5217;&#x8868;&#x3002;</p>
</li>
<li><p>getElementById(int id)
&#x53C2;&#x6570;&#x8868;&#x793A;&#x6587;&#x6863;&#x4E2D;&#x5143;&#x7D20;&#x7684;id&#xFF0C;&#x6587;&#x6863;&#x4E2D;&#x5404;&#x4E2A;&#x5143;&#x7D20;&#x6240;&#x62E5;&#x6709;&#x7684;id&#x5C5E;&#x6027;&#x662F;&#x552F;&#x4E00;&#x7684;&#xFF0C;&#x901A;&#x8FC7;&#x8BE5;&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x627E;&#x5230;&#x67D0;&#x4E2A;id&#x6240;&#x5BF9;&#x5E94;&#x7684;&#x8282;&#x70B9;&#x3002;</p>
</li>
</ul>
<p><strong>Node&#x7C7B;</strong>&#xFF1A;</p>
<blockquote>
<p>Node&#x7C7B;&#x662F;&#x4E3B;&#x8981;&#x63CF;&#x8FF0;xml&#x6587;&#x6863;&#x4E2D;&#x5143;&#x7D20;&#x7684;&#x4E00;&#x4E2A;&#x7C7B;&#xFF0C;&#x901A;&#x8FC7;Node&#x5BF9;&#x8C61;&#xFF0C;&#x53EF;&#x4EE5;&#x83B7;&#x53D6;&#x5230;&#x5F53;&#x524D;&#x8282;&#x70B9;&#x7684;&#x7236;&#x8282;&#x70B9;&#xFF0C;&#x5B50;&#x8282;&#x70B9;&#x5217;&#x8868;&#xFF0C;&#x5C5E;&#x6027;&#x503C;&#xFF0C;&#x6587;&#x672C;&#x503C;&#x7B49;&#x7B49;</p>
</blockquote>
<p><strong>&#x4E3B;&#x8981;&#x4F7F;&#x7528;&#x7684;&#x65B9;&#x6CD5;&#x6709;&#xFF1A;</strong></p>
<ul>
<li><p>getNodeType()
&#x901A;&#x8FC7;&#x6B64;&#x65B9;&#x6CD5;&#x80FD;&#x591F;&#x83B7;&#x53D6;&#x5230;&#x5F53;&#x524D;Node&#x5BF9;&#x8C61;&#x6240;&#x8868;&#x793A;&#x7684;&#x8282;&#x70B9;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x4F7F;&#x7528;dom&#x65B9;&#x5F0F;&#x89E3;&#x6790;&#x65F6;&#xFF0C;&#x6587;&#x6863;&#x4E2D;&#x7684;&#x6587;&#x672C;&#x533A;&#x57DF;&#x4EE5;&#x53CA;&#x7A7A;&#x683C;&#x4E5F;&#x5C5E;&#x4E8E;&#x4E00;&#x79CD;Node&#xFF0C;&#x5176;NodeType&#x503C;&#x4E3A;Node.TEXT_NODE,&#x5176;&#x4F59;&#x7684;NodeType&#x8FD8;&#x6709;Node.ELEMENT_NODE&#x7B49;&#x7B49;&#x3002;</p>
</li>
<li><p>getChildNodes()
 &#x8C03;&#x7528;&#x6B64;&#x65B9;&#x6CD5;&#xFF0C;&#x4F1A;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;NodeList&#xFF0C;&#x4EE3;&#x8868;&#x7684;&#x662F;&#x5F53;&#x524D;&#x8282;&#x70B9;&#x4E0B;&#x7684;&#x6240;&#x6709;&#x5B50;&#x8282;&#x70B9;&#x5217;&#x8868;&#x3002;</p>
</li>
<li><p>getTextContent()
&#x8C03;&#x7528;&#x6B64;&#x65B9;&#x6CD5;&#xFF0C;&#x8FD4;&#x56DE;xml&#x6587;&#x4EF6;&#x4E2D;&#xFF0C;&#x88AB;&#x5F53;&#x524D;&#x8282;&#x70B9;&#x5305;&#x88F9;&#x7684;&#x6587;&#x672C;&#x5185;&#x5BB9;&#x3002;</p>
</li>
<li><p>getNodeName
&#x83B7;&#x53D6;&#x5F53;&#x524D;&#x8282;&#x70B9;&#x7684;&#x540D;&#x79F0;&#x3002;</p>
</li>
</ul>
<p><strong>&#x4FDD;&#x5B58;XML</strong></p>
<blockquote>
<p>&#x5728; TransFormer &#x7C7B;&#x4E2D;&#x6709; transform() &#x65B9;&#x6CD5; &#x80FD;&#x591F;&#x5C06;XML&#x6587;&#x6863;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x4FDD;&#x5B58;</p>
</blockquote>
<p>transform() &#x6709;&#x4E24;&#x4E2A;&#x53C2;&#x6570; Source &#x548C; Result , &#x8FD9;&#x4E2A;&#x4E24;&#x4E2A;&#x90FD;&#x662F;&#x63A5;&#x53E3;&#x90FD;&#x9700;&#x8981;&#x901A;&#x8FC7;&#x5176;&#x5B9E;&#x73B0;&#x7C7B;&#x521B;&#x5EFA; </p>
<pre><code class="lang-java"><span class="hljs-comment">// &#x83B7;&#x53D6;&#x4FDD;&#x5B58;&#x5BF9;&#x8C61;</span>
DOMSource source = <span class="hljs-keyword">new</span> DOMSource(node);
<span class="hljs-comment">// &#x8BBE;&#x7F6E;&#x4FDD;&#x5B58;&#x5BF9;&#x8C61;&#x7684;&#x5730;&#x5740;&#x548C;&#x7F16;&#x7801;</span>
StreamResult result = <span class="hljs-keyword">new</span> StreamResult(<span class="hljs-keyword">new</span> OutputStreamWriter(<span class="hljs-keyword">new</span> FileOutputStream(saveUrl), <span class="hljs-string">&quot;UTF-8&quot;</span>));

<span class="hljs-comment">// &#x521B;&#x5EFA;&#x4FDD;&#x5B58;&#x5DE5;&#x5177;</span>
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transform = transformerFactory.newTransformer();

<span class="hljs-comment">// &#x6267;&#x884C;&#x4FDD;&#x5B58;</span>
transform.transform(source, result);
</code></pre>
<h2 id="dom4j-&#x65B9;&#x5F0F;">dom4j &#x65B9;&#x5F0F;</h2>
<blockquote>
<p>dom4j&#x662F;&#x4E00;&#x4E2A;&#x7B2C;&#x4E09;&#x65B9;&#x5E93; &#x9700;&#x8981;&#x81EA;&#x5DF1;&#x6DFB;&#x52A0;&#x5E93; </p>
<p>&#x4E0B;&#x8F7D;&#x6E90;: <a href="https://dom4j.github.io/" target="_blank">https://dom4j.github.io/</a></p>
</blockquote>
<p><strong>dom4j&#x4F7F;&#x7528;&#x6B65;&#x9AA4;</strong></p>
<ol>
<li>&#x5E38;&#x89C1;&#x89E3;&#x6790;&#x5668;&#x5BF9;&#x8C61;</li>
<li>&#x4F7F;&#x7528;&#x89E3;&#x6790;&#x5668;&#x5BF9;&#x8C61;&#x8BFB;&#x53D6;XML&#x6587;&#x6863;&#x751F;&#x6210;Docuement&#x5BF9;&#x8C61;</li>
<li>&#x6839;&#x636E;Document&#x5BF9;&#x8C61;&#x83B7;&#x53D6;XML&#x7684;&#x5143;&#x7D20;(&#x6807;&#x7B7E;)&#x4FE1;&#x606F;</li>
</ol>
<h3 id="&#x83B7;&#x53D6;xml">&#x83B7;&#x53D6;XML</h3>
<pre><code class="lang-java"><span class="hljs-keyword">import</span> org.dom4j.Document;
<span class="hljs-keyword">import</span> org.dom4j.DocumentException;
<span class="hljs-keyword">import</span> org.dom4j.io.SAXReader;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Foo</span> </span>{

    <span class="hljs-function"><span class="hljs-keyword">public</span> Document <span class="hljs-title">parse</span><span class="hljs-params">(URL url)</span> <span class="hljs-keyword">throws</span> DocumentException </span>{
        <span class="hljs-comment">// &#x521B;&#x5EFA;&#x8BFB;&#x53D6;&#x5BF9;&#x8C61;</span>
        SAXReader reader = <span class="hljs-keyword">new</span> SAXReader();
        <span class="hljs-comment">// &#x4F20;&#x5165;url&#x76F4;&#x63A5;&#x8BFB;&#x53D6;</span>
        Document document = reader.read(url);
        <span class="hljs-keyword">return</span> document;
    }
}
</code></pre>
<p><strong>Dom4j&#x5E38;&#x7528;API</strong>
org.dom4j.Document&#x5E38;&#x7528;&#x65B9;&#x6CD5;</p>
<pre><code>Element getRootElement(); &#x83B7;&#x53D6;XML&#x6587;&#x4EF6;&#x7684;&#x6839;&#x8282;&#x70B9;
</code></pre><p>org.don4j.Element&#x7684;&#x5E38;&#x7528;&#x65B9;&#x6CD5;</p>
<pre><code class="lang-java"><span class="hljs-function">String <span class="hljs-title">getName</span><span class="hljs-params">()</span></span>; <span class="hljs-comment">// &#x8FD4;&#x56DE;&#x6807;&#x7B7E;&#x7684;&#x540D;&#x79F0;</span>
List&lt;Element&gt; elements; <span class="hljs-comment">// &#x83B7;&#x53D6;&#x6240;&#x6709;&#x7684;&#x5B50;&#x6807;&#x7B7E;&#x5E76;&#x653E;&#x5230;List&#x96C6;&#x5408;&#x4E2D;</span>
<span class="hljs-function">String <span class="hljs-title">attributeValue</span><span class="hljs-params">(String name)</span></span>; <span class="hljs-comment">// &#x901A;&#x8FC7;&#x5C5E;&#x6027;&#x540D;&#x79F0;&#x83B7;&#x53D6;&#x5C5E;&#x6027;&#x503C;</span>
<span class="hljs-function">String <span class="hljs-title">getText</span><span class="hljs-params">()</span></span>; <span class="hljs-comment">// g</span>
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="026网络编程Socket.html" class="navigation navigation-prev " aria-label="Previous page: 026网络编程Socket">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="028 注解.html" class="navigation navigation-next " aria-label="Next page: 028 注解">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"027XML解析","level":"2.1.29","depth":2,"next":{"title":"028 注解","level":"2.1.30","depth":2,"path":"books/java/028 注解.md","ref":"books/java/028 注解.md","articles":[]},"previous":{"title":"026网络编程Socket","level":"2.1.28","depth":2,"path":"books/java/026网络编程Socket.md","ref":"books/java/026网络编程Socket.md","articles":[]},"dir":"ltr"},"config":{"plugins":["search-pro","code","back-to-top-button","theme-comscore","intopic-toc","flexible-alerts","expandable-chapters"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"intopic-toc":{"selector":".markdown-section h1, .markdown-section h2, .markdown-section h3, .markdown-section h4, .markdown-section h5, .markdown-section h6","mode":"nested","maxDepth":6,"isCollapsed":true,"isScrollspyActive":true,"visible":true,"label":"In this article"},"search-pro":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"code":{"copyButtons":true},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"theme-comscore":{},"back-to-top-button":{},"flexible-alerts":{"style":"callout","note":{"label":"Note","icon":"fa fa-info-circle","className":"info"},"tip":{"label":"Tip","icon":"fa fa-lightbulb-o","className":"tip"},"warning":{"label":"Warning","icon":"fa fa-exclamation-triangle","className":"warning"},"danger":{"label":"Attention","icon":"fa fa-ban","className":"danger"}},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"expandable-chapters":{}},"theme":"default","author":"作者","lang":"zh-cn","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"isbn":"图书编号","variables":{},"title":"书名","gitbook":"*","description":"描述"},"file":{"path":"books/java/027XML解析.md","mtime":"2022-10-12T06:58:23.974Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2023-03-01T12:24:55.135Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-intopic-toc/anchor.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-intopic-toc/gumshoe.polyfills.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-intopic-toc/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-flexible-alerts/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

